package com.ruoyi.web.controller.course;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.course.domain.CourseTrainSection;
import com.ruoyi.course.domain.CourseWare;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.CourseTrain;
import com.ruoyi.course.service.ICourseTrainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author zzx
 * @date 2023-03-29
 */
@Api("课程")
@RestController
@RequestMapping("/course/course_train")
public class CourseTrainController extends BaseController {

    @Autowired private ICourseTrainService courseTrainService;

    /**
     * 查询课程列表-小程序使用，返回三级菜单，课程->章节->课件
     */
    @GetMapping("/list-all")
    @ApiOperation("查询课程列表")
    public TableDataInfo listAll(CourseTrain courseTrain) {
        startPage();
        courseTrain.setDeptId(getDeptId());
        //courseTrain.setUserId(getUserId());
        List<CourseTrain> list = courseTrainService.selectCourseTrainListAll(courseTrain);
        //将每个课程的第一个课件的缩略图作为课程的缩略图
        for (CourseTrain train : list) {
            List<CourseTrainSection> sectionList = train.getSectionList();
            if (ObjectUtil.isNotEmpty(sectionList)){
                CourseTrainSection courseTrainSection = sectionList.get(0);
                List<CourseWare> courseWareList = courseTrainSection.getCourseWareList();
                if (ObjectUtil.isNotEmpty(courseWareList)){
                    train.setImage(courseWareList.get(0).getThumbnail());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询课程列表
     */
    @GetMapping("/list")
    @ApiOperation("查询课程列表")
    public TableDataInfo list(CourseTrain courseTrain) {
        startPage();
        courseTrain.setDeptId(getDeptId());
        //courseTrain.setUserId(getUserId());
        List<CourseTrain> list = courseTrainService.selectCourseTrainList(courseTrain);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseTrain courseTrain) {
        courseTrain.setDeptId(getDeptId());
        //courseTrain.setUserId(getUserId());
        List<CourseTrain> list = courseTrainService.selectCourseTrainList(courseTrain);
        ExcelUtil<CourseTrain> util = new ExcelUtil<CourseTrain>(CourseTrain.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @ApiOperation("获取课程详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(courseTrainService.selectCourseTrainById(id));
    }

    /**
     * 新增课程
     */
    @ApiOperation("新增课程")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseTrain courseTrain) {
        courseTrain.setDeptId(getDeptId());
        courseTrain.setUserId(getUserId());
        courseTrain.setStatus(0);
        return toAjax(courseTrainService.insertCourseTrain(courseTrain));
    }

    /**
     * 修改课程
     */
    @ApiOperation("修改课程")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseTrain courseTrain) {
        return toAjax(courseTrainService.updateCourseTrain(courseTrain));
    }

    /**
     * 删除课程
     */
    @ApiOperation("删除课程")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(courseTrainService.deleteCourseTrainByIds(ids));
    }
}
